package com.springboot.manager.mybatisPlus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.github.yitter.idgen.YitIdHelper;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @version v1.0
 * @ProjectName: manager
 * @ClassName: MyMetaObjectHandler
 * @Description: 自定义字段自动填充处理类 - 实体类中使用 @TableField注解 注意前端传值时要为null
 * @Author: zhangzl
 * @Date: 2021-07-14 14:45
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    private static final String DEFAULT_OPERATOR = "System";

    /**
     * 创建时间
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        setFieldValByName("id", Long.toString(YitIdHelper.nextId()), metaObject);
        setFieldValByName("createdTime", new Date(), metaObject);
        setFieldValByName("updatedTime", new Date(), metaObject);
        setFieldValByName("createdBy", DEFAULT_OPERATOR, metaObject);
        setFieldValByName("updatedBy", DEFAULT_OPERATOR, metaObject);
        setFieldValByName("isDel", "0", metaObject);
        setFieldValByName("status", "1", metaObject);
    }

    /**
     * 最后一次更新时间
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("updatedTime", new Date(), metaObject);
        setFieldValByName("updatedBy", DEFAULT_OPERATOR, metaObject);
    }

}
